<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\admin\model\ManualModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;

class ManualService
{
    /**
     * 增加操作手册
     * @param $param
     * @return false|string
     */
    public static function addManual($param){
        $param['remark'] = '增加操作手册';
        $data = [
            'name'=>$param['name'],
            'introduce'=>$param['introduce'],
            'author'=>$param['author'],
            'version'=>$param['version'],
            'create_time'=>time(),
            'address'=>$param['address']
        ];
        $int = ManualModel::add($data);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['file'=>'文件','address'=>$param['address']]);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取操作手册
     * @param $param
     * @return false|string
     */
    public static function queryManual($param){
        switch ($param['type']){
            case 1:
                $data = self::handleQueryManual($param);
                break;
            case 2:
                $param['field'] = 'name';
                $data = self::handleQueryManual($param);
                break;
            case 3:
                $param['field'] = 'author';
                $data = self::handleQueryManual($param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理
     * @param $param
     * @return mixed
     */
    public static function handleQueryManual($param){
        $data['count'] = ManualModel::queryCount([],$param);
        $page = ToolUtil::calculationPage($param,$data['count']);
        $data['data'] = ManualModel::queryAll([],$param,$page);
        return $data;
    }

    /**
     * 删除操作手册
     * @param $param
     * @return false|string
     */
    public static function deleteManual($param){
        $param['remark'] = '删除操作手册';
        $manual = ManualModel::queryFirst(['id'=>$param['id']],['address']);
        if($manual){
            $int = ManualModel::delete(['id'=>$param['id']]);
            if($int){
                $param['details'] = ToolUtil::handleSuccessOrError(['file'=>'文件','address'=>$manual['address']]);
                LogUtil::addAdminLog($param);
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该信息');
    }

    /**
     * 下载操作手册调用
     * @param $param
     * @return false|string
     */
    public static function downloadManual($param){
        $param['remark'] = '下载操作手册';
        $manual = ManualModel::queryFirst(['id'=>$param['id']],['download_count','address']);
        if($manual){
            $int = ManualModel::edit(['id'=>$param['id']],['download_count'=>$manual['download_count']+1]);
            if($int){
                $param['details'] = ToolUtil::handleSuccessOrError(['address'=>$manual['address']]);
                return ToolUtil::returnJson(200,'成功',['address'=>$manual['address']]);
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(301,'没有该信息');
    }

}